package com.way.capture.data;

import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;

import com.way.capture.App;
import com.way.capture.utils.AppUtil;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by way on 16/2/1.
 */
public class DataInfo implements Serializable {
    public static final int TYPE_SCREEN_SHOT = 0;
    public static final int TYPE_SCREEN_RECORD = 1;
    private static final String TAG = "DataInfo";
    private static final int COLUMN_ID = 0;
    private static final int COLUMN_PATH = 1;
    private static final int COLUMN_SIZE = 2;
    private static final int COLUMN_DATE = 3;
    private static final int COLUMN_WIDTH = 4;
    private static final int COLUMN_HEIGHT = 5;
    private static final int COLUMN_TITLE = 6;
    private static final String PNG = ".png";
    private static final String GIF = ".gif";
    private static final String MP4 = ".mp4";

    private static final String[] PROJECTIONS = new String[]{
            MediaStore.Files.FileColumns._ID, MediaStore.Files.FileColumns.DATA,
            MediaStore.Files.FileColumns.SIZE, MediaStore.Files.FileColumns.DATE_MODIFIED,
            MediaStore.Files.FileColumns.WIDTH, MediaStore.Files.FileColumns.HEIGHT,
            MediaStore.Files.FileColumns.TITLE
    };

    public long dateModified;
    public long size;
    public long id;
    public String path;
    public String title;
    public int width;
    public int height;

    public boolean isSharedElement = false;
    public int type;

    public DataInfo(Cursor c) {
        id = c.getLong(COLUMN_ID);
        path = c.getString(COLUMN_PATH);
        size = c.getLong(COLUMN_SIZE);
        dateModified = c.getLong(COLUMN_DATE);
        width = c.getInt(COLUMN_WIDTH);
        height = c.getInt(COLUMN_HEIGHT);
        title = c.getString(COLUMN_TITLE);
    }

    public static ArrayList<DataInfo> getDatas(int type) {
        return getDatas();
    }

    public static ArrayList<DataInfo> getDatas() {
        ArrayList<DataInfo> dataInfos = new ArrayList<>();
        ContentResolver contentResolver = App.getContext().getContentResolver();
        String selections = "(" + MediaStore.Files.FileColumns.DATA + " LIKE '%" + AppUtil.SCREENSHOT_FOLDER_PATH + "%')"
                + " and (" + MediaStore.Files.FileColumns.DATA + " LIKE '%" + PNG + "')"
                + " or " +
                "(" + MediaStore.Files.FileColumns.DATA + " LIKE '%" + AppUtil.VIDEOS_FOLDER_PATH + "%')"
                + " and (" + MediaStore.Files.FileColumns.DATA + " LIKE '%" + MP4 + "')";
        Uri queryUri = MediaStore.Files.getContentUri("external");
        Log.d(TAG, "getDatas: selections = " + selections + ", queryUri = " + queryUri);
        Cursor cursor = contentResolver.query(queryUri, PROJECTIONS,
                selections, null, MediaStore.Files.FileColumns.DATE_MODIFIED + " desc");
        if (cursor == null) {
            return dataInfos;
        }

        if (cursor.getCount() == 0) {
            cursor.close();
            return dataInfos;
        }

        cursor.moveToPosition(-1);
        while (cursor.moveToNext()) {
            DataInfo dataInfo = new DataInfo(cursor);
            if (TextUtils.isEmpty(dataInfo.path)) {
                continue;
            }
            dataInfo.type = dataInfo.path.endsWith(PNG) ? TYPE_SCREEN_SHOT : TYPE_SCREEN_RECORD;
            dataInfo.isSharedElement = false;
            dataInfos.add(dataInfo);
        }
        Log.i(TAG, "getScreenshotList cursor.size = " + cursor.getCount());
        cursor.close();
        return dataInfos;
    }

    public static DataInfo getDataInfo(int type, String path) {
        String fileType = type == TYPE_SCREEN_SHOT ? PNG : MP4;
        if (type == -1) {
            fileType = path.endsWith(PNG) ? PNG : MP4;
        }
        Log.i(TAG, "getDataInfo: path = " + path + ", fileType = " + fileType);
        ContentResolver contentResolver = App.getContext().getContentResolver();
        String selections = "(" + MediaStore.Files.FileColumns.DATA + " LIKE '%" + path + "%')"
                + " and (" + MediaStore.Files.FileColumns.DATA + " LIKE '%" + fileType + "')";
        Cursor cursor = contentResolver.query(getContentUriByCategory(fileType), PROJECTIONS,
                selections, null, MediaStore.Files.FileColumns.DATE_MODIFIED + " desc");
        if (cursor == null) {
            return null;
        }
        if (cursor.getCount() == 0) {
            cursor.close();
            return null;
        }
        cursor.moveToFirst();
        DataInfo dataInfo = new DataInfo(cursor);
        dataInfo.type = type;
        dataInfo.isSharedElement = false;
        return dataInfo;
    }

    private static Uri getContentUriByCategory(String cat) {
        Uri uri;
        String volumeName = "external";
        switch (cat) {
            case MP4:
                uri = MediaStore.Video.Media.getContentUri(volumeName);
                break;
            case PNG:
            case GIF:
                uri = MediaStore.Images.Media.getContentUri(volumeName);
                break;
            default:
                uri = null;
        }
        return uri;
    }

    private static List<DataInfo> getScreenshotList() {
        List<DataInfo> dataInfos = new ArrayList<>();
        ContentResolver contentResolver = App.getContext().getContentResolver();
        String selections = "(" + MediaStore.Files.FileColumns.DATA + " LIKE '%" + AppUtil.SCREENSHOT_FOLDER_PATH + "%')"
                + " and (" + MediaStore.Files.FileColumns.DATA + " LIKE '%" + PNG + "')";
        Cursor cursor = contentResolver.query(getContentUriByCategory(PNG), PROJECTIONS,
                selections, null, MediaStore.Files.FileColumns.DATE_MODIFIED + " desc");
        if (cursor == null) {
            return dataInfos;
        }

        if (cursor.getCount() == 0) {
            cursor.close();
            return dataInfos;
        }

        cursor.moveToPosition(-1);
        while (cursor.moveToNext()) {
            DataInfo dataInfo = new DataInfo(cursor);
            dataInfo.type = TYPE_SCREEN_SHOT;
            dataInfo.isSharedElement = false;
            dataInfos.add(dataInfo);
        }
        Log.i(TAG, "getScreenshotList cursor.size = " + cursor.getCount());
        cursor.close();
        return dataInfos;
    }

    private static List<DataInfo> getScreenRecordList() {
        List<DataInfo> dataInfos = new ArrayList<>();
        ContentResolver contentResolver = App.getContext().getContentResolver();
        String selections = "(" + MediaStore.Files.FileColumns.DATA + " LIKE '%" + AppUtil.VIDEOS_FOLDER_PATH + "%')"
                + " and (" + MediaStore.Files.FileColumns.DATA + " LIKE '%" + MP4 + "')";
        Cursor cursor = contentResolver.query(getContentUriByCategory(MP4), PROJECTIONS,
                selections, null, MediaStore.Files.FileColumns.DATE_MODIFIED + " desc");
        if (cursor == null) {
            return dataInfos;
        }

        if (cursor.getCount() == 0) {
            cursor.close();
            return dataInfos;
        }

        cursor.moveToPosition(-1);
        while (cursor.moveToNext()) {
            DataInfo dataInfo = new DataInfo(cursor);
            dataInfo.type = TYPE_SCREEN_RECORD;
            dataInfo.isSharedElement = false;
            dataInfos.add(dataInfo);
        }
        Log.i(TAG, "getScreenRecordList cursor.size = " + cursor.getCount());
        cursor.close();
        return dataInfos;
    }

    public static DataInfo getDataInfo(Context context, Uri uri) {
        ContentResolver contentResolver = App.getContext().getContentResolver();

        Cursor cursor = contentResolver.query(uri, PROJECTIONS,
                null, null, MediaStore.Files.FileColumns.DATE_MODIFIED + " desc");
        if (cursor == null) {
            return null;
        }
        if (cursor.getCount() == 0) {
            cursor.close();
            return null;
        }
        cursor.moveToFirst();
        DataInfo dataInfo = new DataInfo(cursor);
        dataInfo.type = 0;
        dataInfo.isSharedElement = false;
        return dataInfo;
    }
}
